#!/bin/bash

# repo2hdd - copy debian cd/dvd repository to harddisk
# Copyright (C) 2007  Fajran Iman Rusadi

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, U

# version: 0.1

###########################################################################

VERBOSE=0
SOURCE=""
DESTINATION=""
RSYNC="/usr/bin/rsync"
GZIP="/bin/gzip"
ZCAT="/bin/zcat"
CP="/bin/cp"
MKDIR="/bin/mkdir"
RM="/bin/rm"
TMP=/tmp

###########################################################################

function debug {
	if [ "$VERBOSE" = "1" ]; then
		echo $@
	fi
}

function error {
	echo $@ 1>&2
}

###########################################################################

function usage {
	echo "Usage: repo2hdd [OPTION] SOURCE DESTINATION"
	echo "Copy Debian repository from SOURCE to DESTINATION."
	echo ""
	echo "  -v, --verbose         show what is being done"
	echo "  -h, --help            show this message"
	echo ""
	echo "The SOURCE directory should have dists/ and pool/ subdirectory"
	echo "where the files in the repository are placed."
	echo ""
}

###########################################################################
## Check needed applications

if [ ! -f $RSYNC ]; then
	error "repo2hdd needs rsync. Please install it first."
	exit 2;
fi

###########################################################################
## Check parameters

case "$1" in
	-v|--verbose)
		shift
		VERBOSE=1
		;;
	-h|--help)
		shift
		usage
		exit 0
		;;
esac

if [ $# -lt 2 ]; then
	usage
	exit 1
fi

###########################################################################
## Directories 

SOURCE=$1
DESTINATION=$2
SRC_DISTS=$SOURCE/dists
SRC_POOL=$SOURCE/pool
DEST_DISTS=$DESTINATION/dists
DEST_POOL=$DESTINATION/pool

debug "Source directory: $SOURCE"
debug "Destination directory: $DESTINATION"

# check directories

if [ ! -d $SOURCE -o ! -d $SRC_DISTS -o ! -d $SRC_POOL ]; then
	error "Source directory \"$SOURCE\" doesn't exist and/or it doesn't have"
	error "dists/ and/or pool/ subdirectories. Please check it first."
	exit 10;
fi

if [ ! -d $DESTINATION ]; then
	error "Destination directory \"$DESTINATION\" doesn't exist. Create it first."
	exit 11;
fi

###########################################################################
## The process

debug "Create dists/ and pool/ directories in destination directory."
$MKDIR -p $DEST_DISTS
$MKDIR -p $DEST_POOL

# sync the pool/

VERBOSE_OPT=""
if [ "$VERBOSE" = "1" ]; then
	VERBOSE_OPT="-v --progress"
fi

RSYNC_CMD="$RSYNC $VERBOSE_OPT -ax $SRC_POOL/. $DEST_POOL/."

debug "Synchronizing pool/ directory."
$RSYNC_CMD

# sync/append the dists/ content

TMP_FILE=$TMP/.hdd2tmp-`date +%s`
debug "Temporary file is $TMP_FILE"

pushd $SRC_DISTS > /dev/null 2>&1

DISTS_DIR=`find -type f -iregex .\*Packages.gz$ -exec dirname {} \;`

popd > /dev/null 2>&1

for dir in $DISTS_DIR; do

	SRC_PKG=$SRC_DISTS/$dir/Packages.gz
	DEST_PKG=$DEST_DISTS/$dir/Packages.gz

	debug "Checking $DEST_PKG..."
	if [ -f $DEST_PKG ]; then
		debug "Packages.gz file found. Merging is needed."
		$ZCAT $DEST_PKG $SRC_PKG | $GZIP -9 -c > $TMP_FILE
		$CP $TMP_FILE $DEST_PKG
	else
		debug "Packages.gz file is not found. Merging is not needed."
		$MKDIR -p `dirname $DEST_PKG`
		$CP $SRC_PKG $DEST_PKG
	fi

done

if [ -f $TMP_FILE ]; then
	debug "Removing temporary file."
	$RM $TMP_FILE
fi

